package top.rzhsky.controller;

import top.rzhsky.bean.Message;
import top.rzhsky.bean.Type;
import top.rzhsky.bean.User;
import top.rzhsky.server.Config;

import java.io.*;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Listener implements Runnable {

    private static Socket socket;
    public Message message;
    public RegisterController registerController;
    public ImController imController;
    private static ObjectOutputStream oos;
    private ObjectInputStream ois;

    private Logger logger = Logger.getLogger("Listener");

    public Listener(Message message, RegisterController registerController) {
        this.message = message;
        this.registerController = registerController;
    }

    public Listener(Message message, ImController imController) {
        this.message = message;
        this.imController = imController;
    }

    @Override
    public void run() {
        try {
            socket = new Socket(Config.ip, Config.port);
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());

        } catch (IOException e) {
            logger.log(Level.WARNING, e.getMessage());
        }

        logger.info("Connection accepted " + socket.getInetAddress() + ":" + socket.getPort());

        try {
            switch (message.getType()) {
                case LOGIN:
                    write();
                    break;
                case REGISTER:
                    write();
                    break;
            }

            while (socket.isConnected()) {
                Message receiveMessage = (Message) ois.readObject();

                if (receiveMessage != null) {
                    switch (receiveMessage.getType()) {
                        case REGISTER:
                            RegisterController.getInstance().getAccount(receiveMessage);
                            break;
                        case CONNECTED:
                            LoginController.getInstance().intoMain(receiveMessage);
                            break;
                        case LOGINFAIL:
                            LoginController.getInstance().failLogin();
                            break;
                        case DISCONNECTED:
                            imController.setUserList(receiveMessage);
                            break;
                        case HASCONNECTED:
                            LoginController.getInstance().hasConnected(receiveMessage);
                            break;
                        case LOGINUSERLIST:
                            imController.setUserList(receiveMessage);
                            break;
                        case CHAT:
                            imController.addToChat(receiveMessage);
                            break;
                        case GROUPCHAT:
                            imController.addToChatRoom(receiveMessage);
                            break;
                        case SENDFILE:
                            imController.acceptFile(receiveMessage);
                            break;
                        case SENDFAIL:
                            imController.sendFail();
                            break;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            logger.log(Level.WARNING, "文件找不到");
        }

    }

    public static void send(String msg, User fromUser, User toUser,Type type) {
        try {
            Message message = new Message();

            message.setFrom(fromUser);
            message.setTo(toUser);
            message.setType(type);
            message.setContext(msg);
            oos.writeObject(message);
        } catch (IOException e) {
            System.err.println("发送失败");
        }
    }

    public static void sendFile(byte[] fileContext, User fromUser, User toUser,Type type,String fileName) {
        try {
            Message message = new Message();

            message.setFrom(fromUser);
            message.setTo(toUser);
            message.setType(type);
            message.setFileContext(fileContext);
            message.setFileName(fileName);
            oos.writeObject(message);
        } catch (IOException e) {
            System.err.println("发送失败");
        }
    }

    private void write() {
        try {
            oos.writeObject(message);
            oos.flush();
        } catch (IOException e) {
            logger.log(Level.WARNING, e.getMessage());
        }
    }
}
